import json

import requests

from exts import app

class UserService(object):

    @staticmethod
    def getWechatOpenid(code):
        url = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={' \
              '}&grant_type=authorization_code'.format(app.config['APPID'], app.config['APPSECRET'],
                                                       code)
        opresp = json.loads(requests.get(url).text)
        if 'openid' in opresp:
            openid = opresp['openid']
        return openid